package com.cn.kabalifeuser.ui.order.fragment;

import com.cn.kabalifeuser.api.Api;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.callback.CacheCallBack;
import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.order.bean.OrderListAfterSaleBean;
import com.cn.kabalifeuser.ui.order.bean.OrderListListBean;
import com.cn.kabalifeuser.ui.release.bean.ReleaseListBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

/**
 * Created by 晓林 on 2020/9/11.
 * function:
 */
public class MallFragmentsPresenter extends BasePresenter<MallFragmentslView> {

    /**
     * 发货  收货 评价
     *
     * @param token
     * @param status
     * @param page
     * @param limit
     */
    public void onOrderGoodsListData(String token, int status, int page, int limit) {
        OkGo.<OrderListListBean>get(Api.API_ORDER_LIST_GOODS)
                .headers("token", token)
                .params("limit", limit)
                .params("page", page)
                .params("status", status)
                .execute(new CacheCallBack<OrderListListBean>() {
                    @Override
                    public void onSuccess(Response<OrderListListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 售后
     *
     * @param token
     * @param page
     * @param limit
     */
    public void onOrderAfterSaleListData(String token, int page, int limit) {
        OkGo.<OrderListAfterSaleBean>get(Api.API_ORDER_LIST_AFTER)
                .headers("token", token)
                .params("limit", limit)
                .params("page", page)
                .execute(new CacheCallBack<OrderListAfterSaleBean>() {
                    @Override
                    public void onSuccess(Response<OrderListAfterSaleBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderAfterSaleListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 订单的操作   取消
     *
     * @param token
     * @param status
     * @param id
     */
    public void onOrderSetData(String token, int status, int id) {
        OkGo.<MsgBean>post(Api.API_ORDER_SET)
                .headers("token", token)
                .params("id", id)
                .params("status", status)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderSetSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 订单的操作  收货
     *
     * @param token
     * @param id
     */
    public void onOrderSetData(String token, int id) {
        OkGo.<MsgBean>post(Api.API_ORDER_SET_CONFIRM)
                .headers("token", token)
                .params("id", id)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderSetSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 支付宝和微信
     *
     * @param token
     * @param id
     * @param pay_type
     */
    public void onOrderPayData(String token, int id, int pay_type) {
        OkGo.<String>post(Api.API_ORDER_GO_PAY)
                .headers("token", token)
                .params("pay_type", pay_type)
                .params("id", id)
                .execute(new CacheCallBack<String>() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("立即支付：" + response.body());
                        getView().onOrderPaySuccess(response.body());
                    }
                });
    }
}
